package com.kust.orderService.controller;


import com.kust.commomUtils.JwtUtils;
import com.kust.commomUtils.Result;
import com.kust.orderService.service.OrderService;
import com.kust.orderService.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/orderService/pay-log")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @ApiOperation("生成微信支付二维码接口")
    @GetMapping("createNative/{orderNo}")
    public Result createNative(@PathVariable("orderNo") String orderNo){
        //返回信息 包含二维码地址 还有其他信息
        Map map=payLogService.createNative(orderNo);
        return Result.ok().data(map);
    }

    @ApiOperation("查询订单支付状态")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        //调用查询接口
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        if (map == null) {//出错
            return Result.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//如果成功
            //更改订单状态
            payLogService.updateOrderStatus(map);
            return Result.ok().message("支付成功");
        }

        return Result.ok().code(25000).message("支付中");
    }

}

